﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace AlteraxWebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:SideBarListing runat=server></{0}:SideBarListing>")]
    public class SideBarListing : WebControl
    {
        // public SideBarListing() : base(HtmlTextWriterTag.Div) { }
        private string domain;
        private string loginID;
        private bool levelOpened = false;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            // Get rid of generated Span Tags
            StringBuilder objStringBuilder = new StringBuilder();
            StringWriter objStringWriter = new StringWriter(objStringBuilder);
            HtmlTextWriter objHtmlWriter = new HtmlTextWriter(objStringWriter);

            String NTLogin = HttpContext.Current.Request.ServerVariables["LOGON_USER"].ToString();
            // String NTLogin = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
            String[] authDets;
            String delim = "\\";

            authDets = NTLogin.Split(delim.ToCharArray());
            if (authDets.Length == 2)
            {
                domain = authDets[0].Trim();
                loginID = authDets[1].Trim();
            }

            base.Render(objHtmlWriter);

            string strHTML;
            strHTML = Regex.Replace(objStringBuilder.ToString(), "<span[^>]*>", "");
            strHTML = strHTML.Replace("</span>", "");

            writer.Write(strHTML);

            objStringWriter.Close();
            objHtmlWriter.Close();
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            Traverse(output, SiteMap.RootNode, 1, false);

        }

        private void Traverse(HtmlTextWriter output, SiteMapNode node, int level, bool ulTag)
        {
            string siteMapRole = String.Empty;
            if (node.Roles.Count != 0)
                siteMapRole = node.Roles[0].ToString();

            if (level == 1)
            {
            }
            else if (level == 2)
            {
                if (siteMapRole == "" || siteMapRole == String.Empty || siteMapRole == "*" || Roles.IsUserInRole(loginID, siteMapRole))
                {
                    output.RenderBeginTag(HtmlTextWriterTag.H3);
                    output.AddAttribute("href", node.Url);
                    output.RenderBeginTag(HtmlTextWriterTag.A);
                    output.Write(node.Description);
                    output.RenderEndTag();
                    output.RenderEndTag();
                    output.AddAttribute("class", "sidemenu");
                    output.RenderBeginTag(HtmlTextWriterTag.Ul);
                    levelOpened = true;
                }
                else
                    levelOpened = false;
            }
            else
            {
                if (siteMapRole == "" || siteMapRole == String.Empty || siteMapRole == "*" || Roles.IsUserInRole(loginID, siteMapRole))
                {
                    output.RenderBeginTag(HtmlTextWriterTag.Li);
                    output.AddAttribute("href", node.Url);
                    if(node.Url.EndsWith("?"))
                    {
                         output.AddAttribute("target", "_blank");
                    }

                    // output.AddAttribute("runat", "server");
                    output.RenderBeginTag(HtmlTextWriterTag.A);
                    output.AddAttribute("style", "background: none;");
                    output.RenderBeginTag(HtmlTextWriterTag.Span);
                    output.Write(node.Title);
                    output.RenderEndTag();
                    output.RenderEndTag();
                    output.RenderEndTag();
                    output.WriteLine();
                }
            }

            if (node.HasChildNodes)
                level++;

            foreach (SiteMapNode n in node.ChildNodes)
            {
                if (siteMapRole == "" || siteMapRole == String.Empty || siteMapRole == "*" || Roles.IsUserInRole(loginID, siteMapRole))
                {
                    Traverse(output, n, level, ulTag);

                }

                if (siteMapRole == "" || siteMapRole == String.Empty || siteMapRole == "*" || Roles.IsUserInRole(loginID, siteMapRole))
                {
                    if (level == 2)
                    {
                        if(levelOpened)
                            output.RenderEndTag();
                    }
                }
            }
        }
    }
}
